<?php
//排序相关
class lib_position{
	private $model;
	
	private $id;
	
	private $row;
	
	private $min_position,$max_position;
	
	private $current_position;
	
	public static function factory($model,$id = null){
		$_instance = new self;
		$_instance->model = MYSQL::factory($model);
		if (!$_instance->model->field_exists('position')){
			common::stop('模型缺少排序字段');
		}
		
		$min_and_max = $_instance->model->select(array('min(`position`)','min_position'),array('max(`position`)','max_position'))->exec();
		$_instance->min_position = (int)$min_and_max[0]['min_position'];
		$_instance->max_position = (int)$min_and_max[0]['max_position'];
		
		if ($id){
			$_instance->row = $_instance->model->where('id','=',$id)->limit(1)->select()->exec();
			if ($_instance->row){
				$_instance->row = $_instance->row[0];
				$_instance->id = $_instance->row['id'];
				$_instance->current_position = $_instance->row['position'];
			}else{
				return $this->model->error();
			}
		}
		
		return $_instance;
	}
	
	public function find($id){
		$this->row = $this->model->where('id','=',$id)->limit(1)->select()->exec();
		if ($this->row){
			$this->row = $this->row[0];
			$_instance->id = $_instance->row['id'];
			$this->current_position = $this->row['position'];
		}else{
			return $this->model->error();
		}
		return $this;
	}
	
	public function down(){
		if (!$this->row){
			lib_message::message('未载入行');
			return false;
		}
		if ($this->current_position == $this->max_position){
			lib_message::message('已经是最低位');
			return false;
		}
		$switch_brother = $this->model->where('position','>=',$this->current_position)->where('id','!=',$this->id)->limit(1)->select('id','position')->exec();
		if ($switch_brother){
			$brother_id = $switch_brother[0]['id'];
			$brother_position = $switch_brother[0]['position'];
		}
		$self = $this->model->where('id','=',$this->id)->values(array('position'=>$brother_position))->update()->exec();
		$brother = $this->model->where('id','=',$brother_id)->values(array('position'=>$this->current_position))->update()->exec();
		return ($self && $brother);
	}
	
	public function up(){
		if (!$this->row){
			lib_message::message('未载入行');
			return false;
		}
		if ($this->current_position == $this->min_position){
			lib_message::message('已经是最高位');
			return false;
		}
		$switch_brother = $this->model->where('position','<=',$this->current_position)->where('id','!=',$this->id)->order_by('position','desc')->limit(1)->select('id','position')->exec();
		if ($switch_brother){
			$brother_id = $switch_brother[0]['id'];
			$brother_position = $switch_brother[0]['position'];
		}
		$self = $this->model->where('id','=',$this->id)->values(array('position'=>$brother_position))->update()->exec();
		$brother = $this->model->where('id','=',$brother_id)->values(array('position'=>$this->current_position))->update()->exec();
		return ($self && $brother);
	}
	
	public function min_position(){
		return $this->min_position;
	}
	public function max_position(){
		return $this->max_position;
	}
	public function new_position(){
		return $this->max_position+1;
	}
	
}